home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / TakFlying.as < prev    next >
Encoding:
Text File  |  2007-09-27  |  8.9 KB  |  317 lines

  1. class TakFlying extends Library.State
  2. {
  3.    var bGoingLeft;
  4.    var bGoingRight;
  5.    var oKeysManager;
  6.    var nFollowY;
  7.    var nSpeedX;
  8.    var nSpeedY;
  9.    var bFlyingDown;
  10.    var oFlappingSound;
  11.    var mcRef;
  12.    var nLeftLimit;
  13.    var nRightLimit;
  14.    var nSpeedXMaxModifier;
  15.    var nSpeedYMaxModifier;
  16.    var sState;
  17.    static var oCtrl;
  18.    static var UP_LEFT_LIMIT = 50;
  19.    static var UP_RIGHT_LIMIT = 550;
  20.    static var DOWN_LEFT_LIMIT = 50;
  21.    static var DOWN_RIGHT_LIMIT = 550;
  22.    static var START_SPEED = 60;
  23.    static var SPEED_LOOSE = 0.35;
  24.    static var GRAVITY_GAIN = 0.85;
  25.    static var MAX_DROP_SPEED = 20;
  26.    static var MAX_SPEED_AIR = 8;
  27.    static var ACCELERATION_AIR = 0.75;
  28.    static var DIRECTION_CHANGE_FACTOR_AIR = 0.85;
  29.    function TakFlying(__mcRef)
  30.    {
  31.       super(__mcRef);
  32.       this.bGoingLeft = false;
  33.       this.bGoingRight = false;
  34.       this.oKeysManager = new Library.Utils.KeysManager();
  35.       this.oKeysManager.setListenerForKey(this,38);
  36.       this.oKeysManager.setListenerForKey(this,40);
  37.       this.oKeysManager.setListenerForKey(this,37);
  38.       this.oKeysManager.setListenerForKey(this,39);
  39.       this.nFollowY = -50;
  40.       this.nSpeedX = 0;
  41.       this.nSpeedY = - TakFlying.START_SPEED;
  42.       this.setState("FlyUp");
  43.       MiniGameManager.Instance.doAddListener(this);
  44.    }
  45.    static function get Instance()
  46.    {
  47.       return TakFlying.oCtrl;
  48.    }
  49.    function onKeyManagerEvent(__nEvent, __nCode)
  50.    {
  51.       if(!this.Paused)
  52.       {
  53.          switch(__nEvent)
  54.          {
  55.             case Library.Utils.KeysManager.EVENT_KEY_DOWN:
  56.                switch(__nCode)
  57.                {
  58.                   case 37:
  59.                      this.bGoingLeft = true;
  60.                      break;
  61.                   case 39:
  62.                      this.bGoingRight = true;
  63.                      break;
  64.                   case 38:
  65.                      if(this.bFlyingDown)
  66.                      {
  67.                         this.setState("FlyDownSlow");
  68.                      }
  69.                      break;
  70.                   case 40:
  71.                      if(this.bFlyingDown)
  72.                      {
  73.                         this.setState("FlyDownFast");
  74.                      }
  75.                }
  76.                break;
  77.             case Library.Utils.KeysManager.EVENT_KEY_UP:
  78.                switch(__nCode)
  79.                {
  80.                   case 37:
  81.                      this.bGoingLeft = false;
  82.                      break;
  83.                   case 39:
  84.                      this.bGoingRight = false;
  85.                      break;
  86.                   case 38:
  87.                      if(this.bFlyingDown)
  88.                      {
  89.                         if(this.oKeysManager.isKeyDown(40))
  90.                         {
  91.                            this.setState("FlyDownFast");
  92.                         }
  93.                         else
  94.                         {
  95.                            this.setState("FlyDown");
  96.                         }
  97.                      }
  98.                      break;
  99.                   case 40:
  100.                      if(this.bFlyingDown)
  101.                      {
  102.                         if(this.oKeysManager.isKeyDown(38))
  103.                         {
  104.                            this.setState("FlyDownSlow");
  105.                         }
  106.                         else
  107.                         {
  108.                            this.setState("FlyDown");
  109.                         }
  110.                      }
  111.                }
  112.          }
  113.       }
  114.    }
  115.    function doSoundEvent(__nEvent, __oSound)
  116.    {
  117.       if(__nEvent === Library.Sound.SoundManager.EVENT_SOUND_COMPLETE)
  118.       {
  119.          if(__oSound == this.oFlappingSound)
  120.          {
  121.             delete this.oFlappingSound;
  122.          }
  123.       }
  124.    }
  125.    function doPause()
  126.    {
  127.       super.doPause();
  128.       this.oFlappingSound.doPause();
  129.    }
  130.    function doResume()
  131.    {
  132.       super.doResume();
  133.       this.oFlappingSound.doResume();
  134.    }
  135.    function doDestroy()
  136.    {
  137.       this.oKeysManager.doDestroy();
  138.       delete this.oKeysManager;
  139.       delete TakFlying.oCtrl;
  140.       MiniGameManager.Instance.doRemoveListener(this);
  141.       this.oFlappingSound.doDestroy();
  142.       delete this.oFlappingSound;
  143.    }
  144.    function get FollowY()
  145.    {
  146.       return this.nFollowY;
  147.    }
  148.    function get Ref()
  149.    {
  150.       return this.mcRef;
  151.    }
  152.    function doFlyUp()
  153.    {
  154.       this.doMoveY();
  155.       this.nLeftLimit = TakFlying.UP_LEFT_LIMIT;
  156.       this.nRightLimit = TakFlying.UP_RIGHT_LIMIT;
  157.       this.nSpeedXMaxModifier = -2;
  158.       this.doMoveX();
  159.       this.bFlyingDown = false;
  160.       if(this.nSpeedY >= -2)
  161.       {
  162.          this.setState("FlyChange");
  163.       }
  164.    }
  165.    function doFlyChange()
  166.    {
  167.       this.doMoveY();
  168.       this.doMoveX();
  169.       if(this.isStateComplete())
  170.       {
  171.          if(this.oKeysManager.isKeyDown(40))
  172.          {
  173.             this.setState("FlyDownFast");
  174.          }
  175.          else if(this.oKeysManager.isKeyDown(38))
  176.          {
  177.             this.setState("FlyDownSlow");
  178.          }
  179.          else
  180.          {
  181.             this.setState("FlyDown");
  182.          }
  183.       }
  184.    }
  185.    function doFlyDownSlow()
  186.    {
  187.       this.nSpeedYMaxModifier = -10;
  188.       this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,1.5);
  189.       this.doFall();
  190.    }
  191.    function doFlyDownFast()
  192.    {
  193.       this.nSpeedYMaxModifier = 50;
  194.       this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,2.5);
  195.       this.doFall();
  196.    }
  197.    function doFlyDown()
  198.    {
  199.       this.nSpeedYMaxModifier = 0;
  200.       this.doFall();
  201.    }
  202.    function doFall()
  203.    {
  204.       this.doMoveY();
  205.       this.nLeftLimit = TakFlying.DOWN_LEFT_LIMIT;
  206.       this.nRightLimit = TakFlying.DOWN_RIGHT_LIMIT;
  207.       this.nSpeedXMaxModifier = 0;
  208.       this.doMoveX();
  209.       this.bFlyingDown = true;
  210.    }
  211.    function doMoveY()
  212.    {
  213.       if(this.nSpeedY < 0)
  214.       {
  215.          this.nSpeedY += TakFlying.SPEED_LOOSE;
  216.       }
  217.       else
  218.       {
  219.          this.nSpeedY += TakFlying.GRAVITY_GAIN;
  220.          if(this.nFollowY <= -230)
  221.          {
  222.             this.nFollowY = -230;
  223.          }
  224.          else
  225.          {
  226.             this.nFollowY -= 5;
  227.          }
  228.       }
  229.       if(this.nSpeedY > TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier)
  230.       {
  231.          this.nSpeedY = Library.Utils.MoreMath.getReachNum(this.nSpeedY,TakFlying.MAX_DROP_SPEED + this.nSpeedYMaxModifier,0.5);
  232.       }
  233.       if(this.mcRef._y + this.nSpeedY < 0)
  234.       {
  235.          this.mcRef._y += this.nSpeedY;
  236.       }
  237.       else
  238.       {
  239.          this.setState("FlyDown");
  240.          this.doLockState();
  241.          this.mcRef._y += this.nSpeedY;
  242.          MiniGameManager.Instance.doMinigameEnd();
  243.       }
  244.    }
  245.    function doMoveX()
  246.    {
  247.       if(this.bGoingRight)
  248.       {
  249.          if(this.nSpeedX < 0)
  250.          {
  251.             this.nSpeedX *= TakFlying.DIRECTION_CHANGE_FACTOR_AIR;
  252.          }
  253.          if(this.nSpeedX < TakFlying.MAX_SPEED_AIR + this.nSpeedXMaxModifier)
  254.          {
  255.             this.nSpeedX += TakFlying.ACCELERATION_AIR;
  256.          }
  257.       }
  258.       else if(this.bGoingLeft)
  259.       {
  260.          if(this.nSpeedX > 0)
  261.          {
  262.             this.nSpeedX *= TakFlying.DIRECTION_CHANGE_FACTOR_AIR;
  263.          }
  264.          if(this.nSpeedX > - (TakFlying.MAX_SPEED_AIR + this.nSpeedXMaxModifier))
  265.          {
  266.             this.nSpeedX -= TakFlying.ACCELERATION_AIR;
  267.          }
  268.       }
  269.       else
  270.       {
  271.          this.nSpeedX = Library.Utils.MoreMath.getReachZero(this.nSpeedX,TakFlying.ACCELERATION_AIR);
  272.       }
  273.       if(this.mcRef._x + this.nSpeedX > this.nLeftLimit && this.mcRef._x + this.nSpeedX < this.nRightLimit)
  274.       {
  275.          this.mcRef._x += this.nSpeedX;
  276.       }
  277.       else
  278.       {
  279.          this.nSpeedX = 0;
  280.       }
  281.    }
  282.    function doLoadStateAction()
  283.    {
  284.       super.doLoadStateAction();
  285.       switch(this.sState)
  286.       {
  287.          case "FlyChange":
  288.             MiniGameManager.Instance.doStopWinds();
  289.             break;
  290.          case "FlyDown":
  291.             MiniGameManager.Instance.doStartWindNormal();
  292.             if(this.oFlappingSound != undefined)
  293.             {
  294.                this.oFlappingSound.setFadeRate(15);
  295.                this.oFlappingSound.doFadeTo(0);
  296.             }
  297.             break;
  298.          case "FlyDownFast":
  299.             MiniGameManager.Instance.doStartWindFast();
  300.             if(this.oFlappingSound != undefined)
  301.             {
  302.                this.oFlappingSound.setFadeRate(15);
  303.                this.oFlappingSound.doFadeTo(0);
  304.             }
  305.             break;
  306.          case "FlyDownSlow":
  307.             MiniGameManager.Instance.doStartWindSlow();
  308.             if(this.oFlappingSound == undefined)
  309.             {
  310.                this.oFlappingSound = Library.Sound.SoundManager.doPlaySoundInCat(Main.SOUND_CAT_SOUND,"SkySlow.wav",10,999);
  311.                this.oFlappingSound.doAddListener(this);
  312.             }
  313.             this.oFlappingSound.doFadeTo(60);
  314.       }
  315.    }
  316. }
  317.